package app.netant.book.wen56;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
import app.netant.book.xiabook.XiaBookDetail;
/**
 * 
 * @author JiaJ
 *
 */
public class Wen56Detail extends BaseDetail {

	public Wen56Detail(String netSite) {
		super(netSite);
		this.preHref = "http://www.56wen.com";
	}
	
	private String exp = ".txt";

	@Override
	public void analyseTitle() {
		Elements activePart = document.select(".container .main-wrap #colmh .detail .cont h1");
		if(activePart.size()>0){
			String title = activePart.get(0).text();			
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
		}
	}

	@Override
	public void analyseTags() {
		Elements activePart = document.select(".container .main-wrap #colmh .detail .cont .info");
		if(activePart.size()>0){
			String tags = activePart.get(0).text();
			this.splitTags(tags, true);
		}
	}

	@Override
	public void analyseMemo() {
		Elements activePart = document.select(".container .main-wrap #colmh .detail .cont #xs-text #part-intro");
		if(activePart.size()>0){
			String memo = activePart.get(0).text();
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}

	@Override
	public void analyseHref() {
		Elements activePart = document.select(".container .main-wrap #colmh .list-content .fi-list .down .downlinks a.albumdown");
		for(int i=0;i<activePart.size();i++){
			String refer = activePart.get(i).attr("href");
			if(refer.startsWith("/")){
				refer = this.preHref + refer;
			}
					
			String text =  activePart.get(i).text();
			if(text.indexOf("下载到电脑")>0){
				
				System.out.println("【Refer】："+refer);
				
				Wen56Detail detail = new Wen56Detail(refer);
				
				String url = detail.stepHref();
				if(url.startsWith("/")){
					url = this.preHref + url;
				}
				System.out.println("【Href】:"+url);
				
				this.entity.setDownPath(url);
				
				String name = this.entity.getNovelName();
				this.entity.setNovelFullName(name+this.exp);
				
				break;
			}
		}
	}
	
	public String stepHref(){
		
		Elements activePart = document.select(".container .zw .xzxx p");
		for(int i=0;i<activePart.size();i++){
			Element pPart = activePart.get(i);
			String ex = pPart.text().trim();
			
			if(ex.startsWith("下载格式")){
				int idx = ex.indexOf("：");
				if(idx>0){
					this.exp ="."+ ex.substring(idx+1).trim().toLowerCase();
					System.out.println("【EXP】"+this.exp);
				}
			}else if(ex.startsWith("点此下载")){
				String href = pPart.select("a").get(0).attr("href");
				return href;
			}
		
		}
		
		throw new RuntimeException("无法获取下载地址");
	}
	
	@Override
	public void analyseType() {
		Elements activePart = document.select(".container .subnav2-wrap .subnav2 .subnav2-inner .crumbnav2 a");
		if(activePart.size()>3){
			String type = activePart.get(3).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
		
		else if(activePart.size()>2){
			String type = activePart.get(2).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
	}
	
	public static void main(String[] args) {
		Wen56Detail detail = new Wen56Detail("http://www.56wen.com/txt/18412.html");
		detail.test();
	}



}
